package com.duang.framework.dao;

import com.duang.framework.dao.Model.Property;

public class MysqlDialectUtils {

	public static String update(Model model) {
		return update(model, null, true);
	}

	/**
	 * @deprecated
	 * @param model
	 * @param fields
	 * @param all
	 * @return
	 */
	protected static String update(Model model, String[] fields, boolean all) {
		StringBuilder sql = new StringBuilder();
		sql.append("update ").append(model.getTableName()).append(" set ");
		// if (all) {
		for (Property property : model.getPropertyList()) {
			sql.append(property.getColumnName()).append("=?,");
		}
		// } else {
		// for (String field : fields) {
		// sql.append(model.getPropertys().get(field).getColumnName())
		// .append("=?,");
		// }
		// }
		sql.deleteCharAt(sql.length() - 1);
		sql.append(" where ").append(model.getPrimaryKey().getColumnName())
				.append("=?");
		return sql.toString();
	}

	public static String update(Model model, String[] fields) {
		return update(model, fields, false);
	}

	public static String getById(Model model, String... whereColumn) {
		StringBuilder sql = new StringBuilder();
		sql.append("select ");
		sql.append(model.getPrimaryKey().getColumnName()).append(",");
		for (Property property : model.getPropertyList()) {
			sql.append(property.getColumnName()).append(",");
		}
		sql.deleteCharAt(sql.length() - 1);
		sql.append(" from ").append(model.getTableName()).append(" ");
		sql.append("where ");
		for (String column : whereColumn) {
			sql.append(column).append("=? and ");
		}
		sql.delete(sql.length() - 4, sql.length());
		return sql.toString();

	}

	/**
	 * @deprecated
	 * @param model
	 * @param whereColumn
	 * @return
	 */
	public static String getById(Model model, String whereColumn) {
		StringBuilder sql = new StringBuilder();
		String alias = "";
		sql.append("select ");
		sql.append(model.getPrimaryKey().getColumnName()).append(",");
		for (Property property : model.getPropertyList()) {
			sql.append(alias).append(property.getColumnName()).append(",");
		}
		sql.deleteCharAt(sql.length() - 1);
		sql.append(" from ").append(model.getTableName()).append(" ")
				.append(alias);
		sql.append("where ").append(alias).append(whereColumn).append("=?");
		return sql.toString();

	}

	public static String getById(Model model, boolean useAlias) {
		return getById(model, model.getPrimaryKey().getColumnName());
	}

	public static String getById(Model model) {
		return getById(model, false);

	}

	public static String delete(Model model) {
		StringBuilder sql = new StringBuilder();
		sql.append("delete from ").append(model.getTableName())
				.append(" where ")
				.append(model.getPrimaryKey().getColumnName()).append("=?");
		return sql.toString();

	}

	public static String save(Model model) {
		String sql = null;
		try {
			// Class<?> clazz = model.getClazz();
			StringBuilder builder = new StringBuilder();
			builder.append("insert into ");
			builder.append(model.getTableName());
			builder.append(" (");
			// StringBuilder nameBuilder = new StringBuilder();
			StringBuilder valueBuilder = new StringBuilder();
			valueBuilder.append(") values(");
			for (Property propTemp : model.getPropertyList()) {
				builder.append(propTemp.getColumnName());
				// nameBuilder.append(clazz.getName());
				// nameBuilder.append(".");
				// nameBuilder.append(propTemp.getFieldName());
				// PropertyCache.getInstance().addPropertyDescriptor(
				// nameBuilder.toString(), propTemp.getFieldName(), clazz);
				valueBuilder.append("?,");
				builder.append(",");
			}
			builder.deleteCharAt(builder.length() - 1);
			valueBuilder.deleteCharAt(valueBuilder.length() - 1);
			valueBuilder.append(")");
			builder.append(valueBuilder);
			sql = builder.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sql;

	}

}
